作者:何霞2502856453_910 | 来源:互联网 | 2023-09-14 06:29
篇首语:本文由编程笔记#小编为大家整理,主要介绍了Git 常用基本命令相关的知识,希望对你有一定的参考价值。
文章目录
- 基本命令
- 仓库相关命令
- 提交相关命令
- 分支相关命令
- tag 相关命令
- 撤销相关命令
- IDEA Git 相关操作
- 仓库相关操作
- 提交相关操作
- 分支相关操作
- tag 相关操作
- 撤销相关操作
- 参考资料
本文主要介绍 Git 常用的基本命令。
基本命令
仓库相关命令
git init
git clone https://github.com/Erik-ly/git-test.git
git config --list
git config --global user.name "xxx"
git config --global user.email "xxx@xxx.com"
提交相关命令
git add test.txt
git add .
git status
git diff
git commit -m "add test file"
git pull
git push
git log
分支相关命令
git branch
git branch -a
git fetch
git checkout -b dev origin/dev
git checkout release
git checkout -b dev
git branch release
git push origin dev
- 删除本地分支(删除其他分支,不能删除当前分支, -D 是强制删除)
git branch -d dev2
比如我在 dev 分支修改了一个bug,提交为 “fix bug-7”,现在需要把 dev 分支的这个代码合并到 release 分支。
首先在需要合并的 commit 所在的分支下使用 git log
查看,找到 “fix bug-7” 的 commit 为 “072a2e524c9fe3d082dfce37762e4a43e200484c”,按 Q 键退出日志查看。
Erik@DESKTOP-M3B942V MINGW64 ~/IdeaProjects/git-test (dev)
$ git log
commit 072a2e524c9fe3d082dfce37762e4a43e200484c (HEAD -> dev, origin/release-1.0, origin/dev, release-1.0)
Author: “liuyang” <liuyangerik&#64;gmail.com>
Date: Sun Jun 13 11:00:36 2021 &#43;0800
fix bug-7
然后切换到 release 分支。
在 release 分支上执行执行 pick 操作
git checkout release
git cherry-pick 072a2e524c9fe3d082dfce37762e4a43e200484c
tag 相关命令
git tag
git tag -a v1.0 -m "v1.0 tag"
git tag -d v1.0
git show v1.0
git push --tags
撤销相关命令
git checkout file_name
git checkout .
- 重置暂存区的指定文件&#xff0c;与上一次commit保持一致&#xff0c;但工作区不变
git reset [file]
- 重置暂存区与工作区&#xff0c;与上一次commit保持一致
git reset --hard
- 重置当前分支的HEAD为指定commit&#xff0c;同时重置暂存区和工作区&#xff0c;与指定commit一致&#xff0c;commit 之后的版本都会丢失
git reset --hard [commit]
- 撤销某次操作&#xff0c;此次操作之前和之后的 commit 都会保留
git revert [commit]
IDEA Git 相关操作
仓库相关操作
原有项目使用 IDEA 打开&#xff0c;依次点击 VCS — Import into Version Control — Create Git Repository…
![](https://img6.php1.cn/3cdc5/9fdb/2be/da47bc8d7ef99feb.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/b2d20dc518c4f2bc.jpeg)
在弹出的对话框中选择这个项目&#xff0c;点击 “OK” 即可。
下面以项目放到 GitHub 为例&#xff0c;首先在 GitHub 上创建项目&#xff0c;
![](https://img6.php1.cn/3cdc5/9fdb/2be/bf11b0e49f515398.jpeg)
然后添加源端仓库&#xff0c;
![](https://img6.php1.cn/3cdc5/9fdb/2be/3db3628abfa56af1.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/a293f8c44d32665b.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/dacc0bfb30900ef7.jpeg)
commit 代码后&#xff0c;提交到远程仓库。
![](https://img6.php1.cn/3cdc5/9fdb/2be/ae69031dbd684833.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/3db3628abfa56af1.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/4be277893863515c.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/ca461a9e849a174f.jpeg)
Name 写为 非 “origin” 的名字&#xff0c;可根据 Git 仓库工具&#xff0c;比如&#xff1a;GitBucket。之后提交代码的时候可选择改仓库提交。
提交相关操作
推荐使用下图红框中标注的按钮拉取更新代码&#xff0c;快捷键 Ctrl &#43; T
&#xff0c;这种会更新全部&#xff0c;拉取新的分支&#xff0c;自动 merge 等&#xff0c;不容易出错。
![](https://img6.php1.cn/3cdc5/9fdb/2be/f08901b843613755.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/73000bfd7a5e7b83.jpeg)
使用快捷键 Ctrl &#43; K
&#xff0c; 提交代码
![](https://img6.php1.cn/3cdc5/9fdb/2be/edae09277ea28f3c.jpeg)
使用快捷键 Ctrl &#43; Shift &#43; K
push 代码
![](https://img6.php1.cn/3cdc5/9fdb/2be/28d9033d48638dac.jpeg)
提交代码步骤
- 更新最新代码&#xff08;快捷键&#xff1a;
Ctrl &#43; T
&#xff09;&#xff0c;将代码更新到最新。 - 修改代码。
- 更新最新代码&#xff08;快捷键&#xff1a;
Ctrl &#43; T
&#xff09;&#xff0c;提交代码之前要先更新代码&#xff0c;在修改代码这段时间内&#xff0c;如果有其他人修改了代码&#xff0c;需要更新&#xff0c;否则可能会覆盖别人的代码&#xff0c;另外这时更新&#xff0c;如果有 Merge 的情况&#xff0c;会自动 Merge&#xff0c;或让自己来 Merge 代码&#xff0c;在提交记录里不会出现大量的 Merge 记录。 - Add 代码&#xff0c;无特殊情况&#xff0c;在项目上执行下 ADD 操作&#xff0c;将自己新加的或之前 Merge 的一些文件 add 进来&#xff1b;
- Commit 代码&#xff08;快捷键&#xff1a;
Ctrl &#43; K
&#xff09;&#xff0c;注意写清楚修改了哪些内容&#xff0c;尽量一个功能一个 commit&#xff0c;可以在 commit 对话框中选择本次要提交的文件&#xff1b; - Push 代码&#xff08;快捷键&#xff1a;
Ctrl &#43; Shift &#43; K
&#xff09;&#xff0c;提交代码时注意将要提交到哪个分支&#xff0c;这里 push 所有 commit&#xff0c;无法选择某个 commit push。
分支相关操作
![](https://img6.php1.cn/3cdc5/9fdb/2be/c4ecefaf43787b78.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/2005c41e47adb33a.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/eca9f9f2e85ce525.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/7209aafa27606183.jpeg)
如在本地 checkout 出一个 “release-1.0” 分支&#xff0c;需要将这个分支推送到远端&#xff0c;则可以使用 Ctrl &#43; Shift &#43; K
进行 Push&#xff0c;在弹出框中看到 ”release-1.0 → origin&#xff1a;release-1.0“&#xff0c;如果不是这样&#xff0c;则可点击分支进行手动修改&#xff08;红框中带有下划线部分均可点击后修改&#xff09;&#xff0c;点击 ”Push“ 按钮进行提交即可。
![](https://img6.php1.cn/3cdc5/9fdb/2be/549865806c783794.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/8ae54d2e2c828526.jpeg)
可以选择本地的分支
![](https://img6.php1.cn/3cdc5/9fdb/2be/a8a1fa6053f2555c.jpeg)
也可以通过 “Select…” 手动输入特定分支&#xff0c;按 Ctrl &#43; Enter
确定
![](https://img6.php1.cn/3cdc5/9fdb/2be/1be3f790ae84ea73.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/fe8684db5c7ef122.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/73523e50a0f62b7e.jpeg)
如果需要 Merge
![](https://img6.php1.cn/3cdc5/9fdb/2be/18685e4a0fe22e81.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/f8861b576e7114d8.jpeg)
tag 相关操作
在某个 commit 又键&#xff0c;选择 “New Tag …”
![](https://img6.php1.cn/3cdc5/9fdb/2be/fa4430c76e9d3a89.jpeg)
然后输入 tag 名称
![](https://img6.php1.cn/3cdc5/9fdb/2be/865a222ec602713f.jpeg)
最后推送到远端&#xff0c;注意要勾选 “Push Tags”
![](https://img6.php1.cn/3cdc5/9fdb/2be/a93c7f0bf0a18466.jpeg)
撤销相关操作
![](https://img6.php1.cn/3cdc5/9fdb/2be/0e650bf2cfa2ba95.jpeg)
![](https://img6.php1.cn/3cdc5/9fdb/2be/d2a4e721d91f7b30.jpeg)
参考资料
- Git 基本操作 | 菜鸟教程&#xff1a;https://www.runoob.com/git/git-basic-operations.html
- git命令大全&#xff1a;https://gist.github.com/Erik-ly/d68cc402fade0ef6b960ca8fc5603949
- 常用 Git 命令清单 - 阮一峰的网络日志&#xff1a;http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
- Git恢复之前版本的两种方法reset、revert&#xff08;图文详解&#xff09;&#xff1a;https://blog.csdn.net/yxlshk/article/details/79944535
- git revert 用法 - demoblog - 博客园&#xff1a;https://www.cnblogs.com/0616–ataozhijia/p/3709917.html
- Git合并特定commits 到另一个分支&#xff1a;https://blog.csdn.net/ybdesire/article/details/42145597
注&#xff1a;本文首发于个人博客网站&#xff1a; http://www.erik-ly.com/